// PlayerManager.js
import { _decorator, Component, Node } from 'cc';
import { Player } from '../Player';
const { ccclass, property } = _decorator;

@ccclass('PlayerManager')
export class PlayerManager extends Component {
    // 单例实例
    static instance = null;

    players: Map<number, Player>;
    // 构造函数
    constructor() {
        super();
        if (PlayerManager.instance) {
            return PlayerManager.instance;
        }
        PlayerManager.instance = this;
    }

    // 示例方法
    getPlayerData() {
        // 你的逻辑
    }




    addPlayer(player: Player) {
        if (this.players.has(player.id)) {
            console.warn(`Player with ID ${player.id} already exists.`);
            return;
        }
        this.players.set(player.id, player);
    }
    removePlayer(id: number) {
        if (!this.players.has(id)) {
            console.warn(`Player with ID ${id} not found.`);
            return;
        }
        this.players.delete(id);
    }

    // 获取所有玩家
    getAllPlayers() {
        return Array.from(this.players.values());
    }

    // 在场景加载时初始化
    onLoad() {
        if (PlayerManager.instance) {
            this.node.destroy(); // 销毁多余的实例
        } else {
            PlayerManager.instance = this;
            // 其他初始化代码
        }
    }
}
